<?php

namespace App\Trip\Orders\BookOrders\Train\SelectedSeats;

use App\Trip\Train\Seat\Seats;

trait TraitParseSelectedSeats
{
    public function getSelectedSeatRemark(): string
    {
        $remark         = "";
        $remarks        = [];
        $selected_seats = $this->getSelectedSeats();
        if (empty($selected_seats)) {
            return '';
        }
        if ($this->seat_type == Seats::SEAT_TYPE_BED) {
            $remark = "选铺:";
            foreach ($selected_seats as $bed) {
                $remarks[] = isset(Seats::TRAIN_BEDS[$bed['seat']]) ? (Seats::TRAIN_BEDS[$bed['seat']]."x".$bed['count']."个") : "";
            }
        } elseif ($this->seat_type == Seats::SEAT_TYPE_SEAT) {
            $remark = "选座:";
            foreach ($selected_seats as $key => $seat) {
                $prefix = $suffix = "";
                if (in_array($key, Seats::SEAT_SIDES)) {
                    $prefix = "靠";
                } else {
                    $suffix = "座";
                }
                $remarks[] = ("{$prefix}".$key."{$suffix}x".$seat['count']."个");
            }
        }
        return !empty($remarks) ? $remark.implode(',', $remarks) : '';
    }


    public function getSelectSeatPrice(): float
    {
        $total_price = 0;
        if (empty($this->getSelectedSeats())) {
            return $total_price;
        }

        foreach ($this->getSelectedSeats() as $seat) {
            $total_price += $seat['count'] * $seat['service_fee'];
        }

        return $total_price;
    }


    public function getSeatsCount(): int
    {
        $seat_counts = 0;
        if (!empty($this->getSelectedSeats())) {
            $seat_counts = collect($this->getSelectedSeats())->sum('count');
        }
        return $seat_counts;
    }
}